package ar.uba.fi.tonyvaliente.commands;

import ar.uba.fi.tonyvaliente.documents.CreateIndexResult;
import ar.uba.fi.tonyvaliente.documents.DocumentManager;

public class CreateIndexCommand extends Command {

	private boolean recognized;
	private int keys;

	public CreateIndexCommand(String[] args) {
		super(args);

		recognized = false;
		keys = 0;

		if (args.length > 0) {
			String command = args[0];
			command = command.toLowerCase();
			if (command.equals("-createindex")) {
				recognized = true;
				if (args.length > 1) {
					keys = Integer.parseInt(args[1]);
				}
			}
		}
	}

	private CommandResult createIndex() {
		CreateIndexResult createIndexResult = DocumentManager.createIndex(keys);
		if (createIndexResult.isSuccess())
			return CommandResult.EXECUTE_SUCCESSFULLY;
		else
			return new CommandResult(true, false, createIndexResult
					.getErrorMessage());
	}

	public CommandResult execute() {
		if (!recognized)
			return CommandResult.NOT_RECOGNIZED;

		if (keys <= 0)
			return new CommandResult(true, false, "Invalid keys number");

		return createIndex();
	}

	public String getSignature() {
		return "-createIndex \"keys per node\"";
	}

	public String getDescription() {
		// TODO: completar
		return "Create new index with specified ...";
	}

	public String[] getExamples() {
		return new String[] { "-createIndex 8" };
	}
}
